<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="本文参考： https:&#x2F;&#x2F;www.jianshu.com&#x2F;p&#x2F;e9b40acb2993 https:&#x2F;&#x2F;blog.csdn.net&#x2F;c413969814&#x2F;article&#x2F;details&#x2F;78843132  缓存就是介于数据库与后台api接口的“中间件”，使用缓存后，以后每次查询的结果放置在缓存中，第二次查询时便可以从缓存中获取数据集而不需要再次查询数据库，若数据有更新时，查询缓存要相应更新。sp">
<meta property="og:type" content="article">
<meta property="og:title" content="springboot中缓存的使用">
<meta property="og:url" content="http://example.com/2021/04/13/springboot%E4%B8%AD%E7%BC%93%E5%AD%98%E7%9A%84%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="yyc&#39;s blog">
<meta property="og:description" content="本文参考： https:&#x2F;&#x2F;www.jianshu.com&#x2F;p&#x2F;e9b40acb2993 https:&#x2F;&#x2F;blog.csdn.net&#x2F;c413969814&#x2F;article&#x2F;details&#x2F;78843132  缓存就是介于数据库与后台api接口的“中间件”，使用缓存后，以后每次查询的结果放置在缓存中，第二次查询时便可以从缓存中获取数据集而不需要再次查询数据库，若数据有更新时，查询缓存要相应更新。sp">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/goatyyc/giteep-page-imgs/raw/master/imgs/20210413173123.png">
<meta property="article:published_time" content="2021-04-13T09:29:38.000Z">
<meta property="article:modified_time" content="2021-04-13T09:32:09.651Z">
<meta property="article:author" content="yyc">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/goatyyc/giteep-page-imgs/raw/master/imgs/20210413173123.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://example.com/2021/04/13/springboot中缓存的使用/"/>





  <title>springboot中缓存的使用 | yyc's blog</title>
  








<meta name="generator" content="Hexo 5.3.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta custom-logo">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">yyc's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术博客</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/13/springboot%E4%B8%AD%E7%BC%93%E5%AD%98%E7%9A%84%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="yyc's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">springboot中缓存的使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-04-13T17:29:38+08:00">
                2021-04-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/springboot/" itemprop="url" rel="index">
                    <span itemprop="name">springboot</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文参考：</p>
<p><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/e9b40acb2993">https://www.jianshu.com/p/e9b40acb2993</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/c413969814/article/details/78843132">https://blog.csdn.net/c413969814/article/details/78843132</a></p>
<blockquote>
<p>缓存就是介于数据库与后台api接口的“中间件”，使用缓存后，以后每次查询的结果放置在缓存中，第二次查询时便可以从缓存中获取数据集而不需要再次查询数据库，若数据有更新时，查询缓存要相应更新。springboot中常用的缓存有本地内存缓存、使用redis、mongodb等nosql作为缓存服务器等等</p>
</blockquote>
<a id="more"></a>

<p><strong>spring的缓存抽象</strong></p>
<p>spring3.1开始定义了Cache和CacheManager接口来统一不同的缓存技术；支持注解</p>
<table>
<thead>
<tr>
<th align="left">Cache</th>
<th>缓存接口，定义缓存操作：RedisCache、EhCacheCache等</th>
</tr>
</thead>
<tbody><tr>
<td align="left">CacheManager</td>
<td>缓存管理器，管理各种缓存（Cache）组件</td>
</tr>
</tbody></table>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@Cacheable  根据方法的请求参数对结果进行缓存</span><br><span class="line">@CacheEvict 清空缓存</span><br><span class="line">@CachePut   缓存更新</span><br><span class="line">@EnableCaching  开启基于注解的缓存</span><br></pre></td></tr></table></figure>


<p>在springboot中调用者可以“透明地”使用缓存，通过@EnableCaching注解</p>
<p>默认情况下，如果没有添加一个确切的缓存库，springboot自动配置使用内存map作为缓存库。一般情况下不建议作为生成环境使用，用来练练手倒是可以。</p>
<h2 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h2><h4 id="1、引入依赖"><a href="#1、引入依赖" class="headerlink" title="1、引入依赖"></a>1、引入依赖</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- EhCache 相关 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-cache<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h4 id="2、启动类添加注解"><a href="#2、启动类添加注解" class="headerlink" title="2、启动类添加注解"></a>2、启动类添加注解</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@MapperScan(&quot;com.learn.site.dao&quot;)</span></span><br><span class="line"><span class="meta">@EnableCaching</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SiteApplication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication app = <span class="keyword">new</span> SpringApplication(SiteApplication.class);</span><br><span class="line">        app.setBannerMode(Banner.Mode.OFF);</span><br><span class="line">        app.run(args);</span><br><span class="line"><span class="comment">//        SpringApplication.run(SiteApplication.class, args);</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="3、服务层的方法添加注解"><a href="#3、服务层的方法添加注解" class="headerlink" title="3、服务层的方法添加注解"></a>3、服务层的方法添加注解</h4><p><strong>注意：</strong></p>
<p>1、当我们要使用<code>root</code>对象的属性作为<code>key</code>时我们也可以将“<code>#root</code>”省略，因为<code>Spring</code>默认使用的就是<code>root</code>对象的属性。 如</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@Cacheable(key &#x3D; &quot;targetClass + methodName +#p0&quot;)</span><br></pre></td></tr></table></figure>
<p>2、使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。 如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Cacheable(value=&quot;users&quot;, key=&quot;#id&quot;)</span></span><br><span class="line"><span class="meta">@Cacheable(value=&quot;users&quot;, key=&quot;#p0&quot;)</span></span><br></pre></td></tr></table></figure>


<p><strong>注解@CacheAble</strong></p>
<p><code>@CacheAble</code> 先查询是否已经有缓存，有则使用缓存而不调用方法，没有则会调用方法并添加缓存</p>
<p>参数：（value=”xxx”, key=”#xx”）</p>
<p>此次的value参数是必需的，指定了缓存存放的命名空间</p>
<p>此处的<code>key</code>是使用的<code>spEL</code>表达式。</p>
<p><code>SpEL</code>提供了多种运算符</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>运算符</th>
</tr>
</thead>
<tbody><tr>
<td>关系</td>
<td>&lt;，&gt;，&lt;=，&gt;=，==，!=，lt，gt，le，ge，eq，ne</td>
</tr>
<tr>
<td>算术</td>
<td>+，- ，* ，/，%，^</td>
</tr>
<tr>
<td>逻辑</td>
<td>&amp;&amp;，||，!，and，or，not，between，instanceof</td>
</tr>
<tr>
<td>条件</td>
<td>?: (ternary)，?: (elvis)</td>
</tr>
<tr>
<td>正则表达式</td>
<td>matches</td>
</tr>
<tr>
<td>其他类型</td>
<td>?.，?[…]，![…]，^[…]，$[…]</td>
</tr>
</tbody></table>
<p><strong>测试：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ContentServiceImpl</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="meta">@Cacheable(value = &quot;articleCaches&quot;, key = &quot;&#x27;articlesByCond_&#x27; + #p1+&#x27;type_&#x27;+#p0.type&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PageInfo&lt;Content&gt; <span class="title">getArticleByCond</span><span class="params">(ContentCond contentCond, <span class="keyword">int</span> pageNum, <span class="keyword">int</span> pageSize)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == contentCond)</span><br><span class="line">            <span class="keyword">throw</span> BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);</span><br><span class="line">        PageHelper.startPage(pageNum,pageSize);</span><br><span class="line">        List&lt;Content&gt; contents = contentMapper.getArticlesByCond(contentCond);</span><br><span class="line">        PageInfo&lt;Content&gt; pageInfo = <span class="keyword">new</span> PageInfo&lt;&gt;(contents);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> pageInfo;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


<p>第二次刷新页面，控制台没有再次打印sql语句，说明缓存成功。</p>
<p>这时候测试一下，数据库添加数据，因为没有更新缓存，应该不会刷新：</p>
<p><img src="https://gitee.com/goatyyc/giteep-page-imgs/raw/master/imgs/20210413173123.png"></p>
<p>多次刷新都并没有出现新的文章，重启应用后才可以看到新增的文章，因此使用缓存要十分小心，要注意避免更新时缓存未刷新的情况出现</p>
<p><strong>补全：</strong></p>
<p><strong>清空缓存</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="meta">@CacheEvict(value = &#123;&quot;articleCache&quot;,&quot;articleCaches&quot;&#125;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteArticleById</span><span class="params">(Integer cid)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">null</span> == cid)</span><br><span class="line">        <span class="keyword">throw</span> BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);</span><br><span class="line"></span><br><span class="line">    contentMapper.deleteArticleById(cid);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 删除标签和分类关联</span></span><br><span class="line">    relationShipMapper.deleteRelationShipById(cid);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">@Override</span><br><span class="line">@CacheEvict(value &#x3D; &#123;&quot;articleCache&quot;,&quot;articleCaches&quot;&#125;, allEntries&#x3D;true, beforeInvocation &#x3D; true)</span><br><span class="line">public void updateArticleById(Content contentPojo) &#123;</span><br><span class="line">    &#x2F;&#x2F; 分类</span><br><span class="line">    String categories &#x3D; contentPojo.getCategories();</span><br><span class="line"></span><br><span class="line">    contentMapper.updateArticleById(contentPojo);</span><br><span class="line">    int cid &#x3D; contentPojo.getCid();</span><br><span class="line">    relationShipMapper.deleteRelationShipById(cid);</span><br><span class="line"></span><br><span class="line">    metaService.addMetas(cid,categories, Types.CATEGORY.getType());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>spring cache中，@CacheEvict是清除缓存的注解，参数value,key的意思是清除在value命名空间中key值数据，默认在当前方法执行成功后清除。存在一个问题：也许该方法成功执行了更新/删除操作，但后续代码抛出异常导致未能清除缓存，下次查询时，旧缓存仍然存在。</p>
<p>一个简单的解决方法：在注解参数加beforeInvocation=true，意思是在执行这个方法之前先执行清除缓存操作。</p>
<p>当注解参数加上allEntries=true时，是说这个清除缓存是清除当前value值空间下的所有缓存数据</p>
<p>到这里已经验证缓存功能的实现</p>
<h2 id="源码"><a href="#源码" class="headerlink" title="源码"></a>源码</h2><p>打开@Cacheable注解的源码，可以看到该注解提供的其他属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Target(&#123;ElementType.TYPE, ElementType.METHOD&#125;)</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@Inherited</span></span><br><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> Cacheable &#123;</span><br><span class="line">    <span class="meta">@AliasFor(&quot;cacheNames&quot;)</span></span><br><span class="line">    String[] value() <span class="keyword">default</span> &#123;&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@AliasFor(&quot;value&quot;)</span></span><br><span class="line">    String[] cacheNames() <span class="keyword">default</span> &#123;&#125;;		<span class="comment">//和value注解差不多，二选一</span></span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">key</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">keyGenerator</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;		<span class="comment">//和value注解差不多，二选一</span></span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">cacheManager</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;		<span class="comment">//指定缓存管理器</span></span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">cacheResolver</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;		<span class="comment">//或者指定获取解析器</span></span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">condition</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;			<span class="comment">//条件符合则缓存</span></span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">unless</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;				<span class="comment">//条件符合则不缓存</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">sync</span><span class="params">()</span> <span class="keyword">default</span> <span class="keyword">false</span></span>;			<span class="comment">//是否使用异步模式</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>在idea中可以下载查看源码</p>
<h3 id="配置-CacheConfig"><a href="#配置-CacheConfig" class="headerlink" title="配置@CacheConfig"></a>配置@CacheConfig</h3><blockquote>
<p>当需要使用缓存的地方越来越多时，可以使用@CacheConfig(cacheNames={“xxx”})注解来统一指定value值，这时可省略value，如果方法依旧写入value，则以方法的value值为准</p>
</blockquote>
<h4 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@CacheConfig(cacheNames = &#123;&quot;myCache&quot;&#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BotRelationServiceImpl</span> <span class="keyword">implements</span> <span class="title">BotRelationService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="meta">@Cacheable(key = &quot;targetClass + methodName +#p0&quot;)</span><span class="comment">//此处没写value</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;BotRelation&gt; <span class="title">findAllLimit</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> botRelationRepository.findAllLimit(num);</span><br><span class="line">    &#125;</span><br><span class="line">    .....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




<h3 id="更新-CachePut"><a href="#更新-CachePut" class="headerlink" title="更新@CachePut"></a>更新<code>@CachePut</code></h3><p> <code>@CachePut</code>注解的作用 主要针对方法配置，能够根据方法的请求参数对其结果进行缓存，和 <code>@Cacheable</code> 不同的是，它每次都会触发真实方法的调用 。简单来说就是用户更新缓存数据。但需要注意的是该注解的<code>value</code> 和 <code>key</code> 必须与要更新的缓存相同，也就是与<code>@Cacheable</code> 相同。</p>
<h4 id="示例-1"><a href="#示例-1" class="headerlink" title="示例"></a>示例</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@CachePut(value = &quot;emp&quot;, key = &quot;targetClass + #p0&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> NewJob <span class="title">updata</span><span class="params">(NewJob job)</span> </span>&#123;</span><br><span class="line">    NewJob newJob = newJobDao.findAllById(job.getId());</span><br><span class="line">    newJob.updata(job);</span><br><span class="line">    <span class="keyword">return</span> job;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Cacheable(value = &quot;emp&quot;, key = &quot;targetClass +#p0&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> NewJob <span class="title">save</span><span class="params">(NewJob job)</span> </span>&#123;</span><br><span class="line">    newJobDao.save(job);</span><br><span class="line">    <span class="keyword">return</span> job;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/04/11/docker-%E5%88%9D%E8%AF%86/" rel="next" title="docker 初识">
                <i class="fa fa-chevron-left"></i> docker 初识
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/04/14/springboot%E4%B8%AD%E4%BA%8B%E5%8A%A1%E7%9A%84%E4%BD%BF%E7%94%A8/" rel="prev" title="springboot中事务的使用">
                springboot中事务的使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.png"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
                <a href="/archives">
                  <span class="site-state-item-count">23</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95"><span class="nav-text">使用方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81%E5%BC%95%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="nav-text">1、引入依赖</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2%E3%80%81%E5%90%AF%E5%8A%A8%E7%B1%BB%E6%B7%BB%E5%8A%A0%E6%B3%A8%E8%A7%A3"><span class="nav-text">2、启动类添加注解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3%E3%80%81%E6%9C%8D%E5%8A%A1%E5%B1%82%E7%9A%84%E6%96%B9%E6%B3%95%E6%B7%BB%E5%8A%A0%E6%B3%A8%E8%A7%A3"><span class="nav-text">3、服务层的方法添加注解</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%BA%90%E7%A0%81"><span class="nav-text">源码</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE-CacheConfig"><span class="nav-text">配置@CacheConfig</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B"><span class="nav-text">示例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0-CachePut"><span class="nav-text">更新@CachePut</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B-1"><span class="nav-text">示例</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yyc</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
